<?php
/**************************
 * Created on Jan 22, 2011
 * By: Jos Pape
 * info: jospape@hotmail.com
 */
include_once "../../../common.php";
include_once $root."/header.php";

/************
 * Functies
 */
function deleteSubCatogorien($catID)
{
	global $_MySQL;
	$query = "SELECT ID FROM ProductCatogorien WHERE CatogorieID = '".str_replace("'","",$catID)."'";
	$result = $_MySQL->runQuery($query);
	while($row = mysql_fetch_assoc($result))
	{
		deleteSubCatogorien($row['ID']);
		$query = "DELETE FROM ProductCatogorien WHERE ID = '".str_replace("'","",$row['ID'])."' LIMIT 1";
		$_MySQL->runQuery($query);
	}
}
	
function getSubCatogorien($catogorieID)
{
	global $_MySQL;	
	$query = "SELECT * FROM ProductCatogorien WHERE CatogorieID = '".str_replace("'","",$catogorieID)."' ORDER BY Naam";
	$result = $_MySQL->runQuery($query);
	?>
	<ul>
		<li>
			<input type="button" value="Toevoegen"  onclick="javascript: $('#catogorie<?=$catogorieID;?>Toevoegen').toggle(true);" />
			<div id="catogorie<?=$catogorieID;?>Toevoegen" style="display:none;">
			<form action="?" method="post">
				Naam: <input type="text" name="Naam" />
				<br /><input type="submit" value=" Opslaan " />
				<input type="hidden" name="action" value="subToevoegen" />
				<input type="hidden" name="subID" value="<?=$catogorieID;?>" />
			</form>
			</div>
		</li>
	<?php
	if(mysql_num_rows($result)>=1)
	{
		while($row = mysql_fetch_assoc($result))
		{
			?>
			<li>
				<?=$row['Naam'];?> 
				<a href="?delete=<?=$row['ID'];?>" onClick="javascript: return window.confirm('Weet je zeker dat je <?=$row['Naam'];?> wilt verwijderen?');">X</a>
				<?php
				getSubCatogorien($row['ID']);
				?>
			</li>
			<?php
		}
	}
	?>
	</ul>
	<?php
}
/*************
 * POST
 */
if(isset($_POST['action']) && $_POST['action'] == "catToevoegen")
{
	if(!isset($_POST['Naam']) || empty($_POST['Naam']))
		$error['Naam'] = "moet verplicht worden opgegeven";
	if(!isset($error))
	{
		// instert
		$query = "INSERT INTO ProductCatogorien (
						CatogorieID, 	 	 	 	 	 	
						Naam
					) VALUES (
						NULL,
						'".str_replace("'","&#39;",$_POST['Naam'])."'
					)";
		$_MySQL->runQuery($query);
	}
	
}
elseif(isset($_POST['action']) && $_POST['action'] == "subToevoegen")
{
	if(!isset($_POST['subID']) || empty($_POST['subID']) || !is_numeric($_POST['subID']))
		$error['Sub catogorie'] = "Moet verplicht worden meegegeven";
	if(!isset($_POST['Naam']) || empty($_POST['Naam']))
		$error['Naam'] = "moet verplicht worden opgegeven";
	if(!isset($error))
	{
		// instert
		$query = "INSERT INTO ProductCatogorien (
						CatogorieID,		 	 	 	 	 	 	
						Naam
					) VALUES (
						'".str_replace("'","",$_POST['subID'])."',
						'".str_replace("'","&#39;",$_POST['Naam'])."'
					)";
		$_MySQL->runQuery($query);
	}
}

if(isset($_GET['delete']) && is_numeric($_GET['delete']))
{
	// delete tha basterdooo
	// subs...
	deleteSubCatogorien($_GET['delete']);
	$query = "DELETE FROM ProductCatogorien WHERE ID = '".str_replace("'","",$_GET['delete'])."' LIMIT 1";
	$_MySQL->runQuery($query);
}
?>
<h1>Product Catogorien</h1>

<?php
if(isset($error) && is_array($error))
{
	?>
	<ul class="error">
	<?php
	foreach($error AS $what => $why)
	{
		?>
		<li><b><?=$what;?></b>: <?=$why;?></li>	
		<?php	
	}
	?>
	</ul>
	<?php
}
$query = "SELECT * FROM ProductCatogorien WHERE CatogorieID IS NULL ORDER BY Naam";
$result = $_MySQL->runQuery($query);
?>
<ul class="catogorien">
	<li>
		<input type="button" value="Toevoegen" onclick="javascript: $('#catogorieToevoegen').toggle(true);"/>
		<div id="catogorieToevoegen" style="display:none;">
		<form action="?" method="post">
			Naam: <input type="text" name="Naam" />
			<input type="hidden" name="action" value="catToevoegen" />
			<br /><input type="submit" value=" Opslaan " />
		</form>
		</div>
	</li>
<?php
while($row = mysql_fetch_assoc($result))
{
	?>
	<li>
		<?=$row['Naam'];?> 
		<a href="?delete=<?=$row['ID'];?>" onClick="javascript: return window.confirm('Weet je zeker dat je <?=$row['Naam'];?> wilt verwijderen?');">X</a>
		<?php
		getSubCatogorien($row['ID']);
		?>
	</li>
	<?php
}
?>
</ul>
<?php
include_once $root."/footer.php";